/*
 *    Copyright 2008-2009 The Digester Tools Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.digestertools.json;

import java.io.IOException;

import lombok.Getter;
import lombok.Setter;

import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;

/**
 * JSON parser that throws XML events.
 *
 * @author Simone Tripodi
 * @author Tommaso Teofili
 * @version $Id: JsonXMLReader.java 370 2009-12-17 09:30:01Z simone.tripodi $
 */
final class JsonXMLReader implements XMLReader {

    private final String customRootElementName;

    @Getter
    @Setter
    private ContentHandler contentHandler;

    @Getter
    @Setter
    private EntityResolver entityResolver;

    @Getter
    @Setter
    private ErrorHandler errorHandler;

    private DTDHandler dtdHandler;

    public JsonXMLReader(final String customRootElementName) {
        this.customRootElementName = customRootElementName;
    }

    /**
     * {@inheritDoc}
     */
    public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
        throw new SAXNotSupportedException();
    }

    /**
     * {@inheritDoc}
     */
    public Object getProperty(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
        throw new SAXNotSupportedException();
    }

    /**
     * {@inheritDoc}
     */
    public DTDHandler getDTDHandler() {
        return this.dtdHandler;
    }

    /**
     * {@inheritDoc}
     */
    public void setDTDHandler(DTDHandler handler) {
        this.dtdHandler = handler;
    }

    /**
     * {@inheritDoc}
     */
    public void parse(final InputSource input) throws IOException, SAXException {
        JsonParser jsonParser = new JsonParser(input.getByteStream());
        if (this.customRootElementName != null && this.customRootElementName.length() > 0) {
            jsonParser.setRootElementName(this.customRootElementName);
        }
        jsonParser.setContentHandler(this.contentHandler);
        try {
            jsonParser.json();
        } catch (ParseException e) {
            throw new SAXException("An error occurred while parsing JSON", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void parse(final String systemId) throws IOException, SAXException {
        // not used
    }

    /**
     * {@inheritDoc}
     */
    public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
        throw new SAXNotSupportedException();
    }

    /**
     * {@inheritDoc}
     */
    public void setProperty(final String name, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
        throw new SAXNotSupportedException();
    }

}
